package com.stone.framework.user.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.user.DeptReqBody;
import com.stone.framework.message.user.DeptTreeResBody;
import com.stone.framework.user.service.DeptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "部门")
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "获取树形结构", httpMethod = "POST")
    @PostMapping(value = "/getTree")
    ResMessage<List<DeptTreeResBody>> getTree(@Valid @RequestBody ReqMessage<Void> reqMessage) {
        List<DeptTreeResBody> list = deptService.buildTree();
        return ResMessage.success(list);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<DeptReqBody> reqMessage) {
        deptService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<DeptReqBody> reqMessage) {
        deptService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        deptService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

}
